﻿using DoNet.Bpmn.Core.Enums;
using DoNet.Bpmn.Core.Vo;
using DoNet.Bpmn.Models;
using DoNet.Bpmn.Services;
using Microsoft.Extensions.Logging;

namespace DoNet.Bpmn.Adaptor.BpmnNode
{
    public class NodePropertyCustomizeAdaptor : BpmnNodeAdaptor
    {
        private readonly BpmnNodeCustomizeConfigService _bpmnNodeCustomizeConfService;
        private readonly ILogger<NodePropertyCustomizeAdaptor> _logger;

        public NodePropertyCustomizeAdaptor(BpmnNodeCustomizeConfigService bpmnNodeCustomizeConfService,
            ILogger<NodePropertyCustomizeAdaptor> logger)
        {
            _bpmnNodeCustomizeConfService = bpmnNodeCustomizeConfService;
            _logger = logger;
        }
        public override BpmnNodeVo? FormatToBpmnNodeVo(BpmnNodeVo bpmnNodeVo)
        {
            BpmnNodeCustomizeConfig customizeConfig = _bpmnNodeCustomizeConfService.GetByBpmnNodeId(bpmnNodeVo.Id);
            bpmnNodeVo.Property = new BpmnNodePropertysVo()
            {
                SignType = customizeConfig.SignType,
            };
            return bpmnNodeVo;
        }

        public override void EditBpmnNode(BpmnNodeVo bpmnNodeVo)
        {
            BpmnNodePropertysVo bpmnNodePropertysVo = bpmnNodeVo.Property ?? new BpmnNodePropertysVo();
            BpmnNodeCustomizeConfig customizeConf = new BpmnNodeCustomizeConfig()
            {
                BpmnNodeId = bpmnNodeVo.Id,
                SignType = bpmnNodePropertysVo.SignType,
            };
            _bpmnNodeCustomizeConfService.Insert(customizeConf);
        }

        public override void SetSupportBusinessObjects()
        {
            ((IAdaptorService)this).AddSupportBusinessObjects(BpmnNodeAdpConfigEnum.ADP_CONF_NODE_PROPERTY_CUSTOMIZE);
        }
    }
}
